package com.itheima.controller;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    @Autowired
    private CheckItemService checkItemService;
    //新增
    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        checkItemService.add(checkItem);
        return new Result(true,"新增检查项成功");
    }
    //根据id删除检查项
    @GetMapping("/delete")
    public Result delete(Integer id){
        int row= checkItemService.delete(id);
        return row > 0 ? new Result(true,"删除检查项成功") : new Result(false,"无法删除已关联检查组的检查项");
    }
   //根据检查组id查询检查项id
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer checkgroupId){
        return new Result(true,"查询检查项成功",checkItemService.findCheckItemIdsByCheckGroupId(checkgroupId));
    }
    //编辑
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        checkItemService.edit(checkItem);
        return new Result(true,"编辑检查项成功");
    }
    //分页查询
    @PostMapping ("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return checkItemService.findPage(queryPageBean);
    }
    //检查所有检查项
    @GetMapping("/findAll")
    public Result findAll(){
        return new Result(true,"查询检查项成功",checkItemService.findAll());
    }
    //根据id查询
    @GetMapping("/findById")
    public Result findById(Integer id){
        return new Result(true,"查询检查项成功",checkItemService.findById(id));
    }
}
